package LeetCode;

import TimeCounter.CglibProxy;

/**
 * 给你两个 **非空** 的链表，表示两个非负的整数。它们每位数字都是按照 **逆序** 的方式存储的，并且每个节点只能存储 **一位** 数字。
 *
 * 请你将两个数相加，并以相同形式返回一个表示和的链表。
 *
 * 你可以假设除了数字 0 之外，这两个数都不会以 0 开头。
 *
 * 输入：l1 = [2,4,3], l2 = [5,6,4]
 * 输出：[7,0,8]
 * 解释：342 + 465 = 807.
 *
 * 输入：l1 = [0], l2 = [0]
 * 输出：[0]
 *
 * 输入：l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
 * 输出：[8,9,9,9,0,0,0,1]
 */
public class Code02 {
    private static class ListNode{
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }

    public ListNode addTwoNumbers1(ListNode l1, ListNode l2) { // 迭代法
        ListNode answerList = new ListNode();
        ListNode currentP = answerList;
        int num = 0;

        while (l1 != null || l2 != null || num != 0){
            currentP.val = ((l1 != null)?l1.val:0) + ((l2 != null)?l2.val:0) + num;
            num = 0;
            if (currentP.val >= 10){
                num = currentP.val / 10;
                currentP.val %= 10;
            }
            System.out.print(" --> currentP = " + currentP.val);
            l1 = l1 != null ? l1.next : null;
            l2 = l2 != null ? l2.next : null;
            if (l1 != null || l2 != null || num != 0){
                currentP.next = new ListNode();
                currentP = currentP.next;
            }
        }
        System.out.println();
        return answerList;
    }

    public ListNode addTwoNumbers2(ListNode l1, ListNode l2) { // 递归法
        return rrun(l1,l2,0);
    }

    public ListNode rrun(ListNode l1, ListNode l2,int num){
        if (l1 == null && l2 == null && num == 0)
            return null;
        ListNode answerList = new ListNode();
        answerList.val = ((l1 != null)?l1.val:0) + ((l2 != null)?l2.val:0) + num;
        num = 0;
        if (answerList.val >= 10){
            num = answerList.val / 10;
            answerList.val %= 10;
        }
        System.out.println("answerList = " + answerList.val);
        l1 = l1 != null ? l1.next : null;
        l2 = l2 != null ? l2.next : null;
        answerList.next = rrun(l1,l2,num);
        return answerList;
    }

    public static void main(String[] args) {
        Code02 code02 = new CglibProxy<Code02>().createProxy(new Code02());
        ListNode l1;
        ListNode l2;
//        l1 = new ListNode(2, new ListNode(4, new ListNode(3)));
//        l2 = new ListNode(5, new ListNode(6, new ListNode(4)));
//        code02.addTwoNumbers1(l1,l2);
//        code02.addTwoNumbers2(l1,l2);

//        l1 = new ListNode(0);
//        l2 = new ListNode(0);
//        code02.addTwoNumbers1(l1,l2);
//        code02.addTwoNumbers2(l1,l2);
//
        l1 = new ListNode(9,new ListNode(9,new ListNode(9,new ListNode(9,new ListNode(9,new ListNode(9,new ListNode(9)))))));
        l2 = new ListNode(9,new ListNode(9,new ListNode(9,new ListNode(9))));
        code02.addTwoNumbers1(l1,l2);
        code02.addTwoNumbers2(l1,l2);
    }

}
